<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="800px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="110px" label-suffix=":">
            <el-form-item label="店铺" prop="shop_id">
                <el-select v-model="model.shop_id" placeholder="请选择店铺" clearable style="width: 240px">
                    <el-option v-for="(item, index) in shopList" :key="index" :label="item.shop_name" :value="item.shop_id" />
                </el-select>
            </el-form-item>
            <el-form-item label="SPU" prop="spu">
                <el-input v-model.trim="model.spu" :placeholder="`请输入SPU`" clearable style="width: 240px"></el-input>
            </el-form-item>
            <!-- <el-form-item label="达人" prop="expert_id">
                <el-select v-model="model.expert_id" placeholder="请选择达人" clearable style="width: 240px"> 
                    <el-option v-for="item in expertList" :key="item.expert_id" :label="item.expert_name" :value="item.expert_id"></el-option>
                </el-select>
            </el-form-item> -->
            <el-form-item label="达人" prop="expert_name">
                <el-input v-model.trim="model.expert_name" :placeholder="`请输入达人`" clearable style="width: 240px"></el-input>
            </el-form-item>
            <el-form-item label="合创码" prop="creation_code">
                <el-input v-model.trim="model.creation_code" :placeholder="`请输入合创码`" clearable style="width: 240px"></el-input>
            </el-form-item>
            <el-form-item label="素材类型" prop="material_type">
                <el-select v-model="model.material_type" placeholder="请选择素材类型" clearable disabled style="width: 240px">
                    <el-option v-for="(item, index) in materialType" :key="index" :label="item.label" :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="网盘地址" prop="material_url">
                <el-input v-model.trim="model.material_url" :placeholder="`请输入网盘地址`" clearable style="width: 240px"></el-input>
            </el-form-item>
            <el-form-item label="发布信息" prop="platform_list">
                <el-table :data="model.platform_list" border script style="width: 600px">
                    <el-table-column label="发布平台" min-width="120">
                        <template slot-scope="scope">
                            <el-select v-model="scope.row.platform_id" placeholder="请选择发布平台" clearable filterable style="width: 100%">
                                <el-option v-for="item in platformList" :key="item.value" :label="item.label" :value="item"></el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="平台发布地址" min-width="200">
                        <template slot-scope="scope">
                            <el-input v-model.trim="scope.row.platform_url" placeholder="请填写平台发布地址" clearable  style="width: 100%"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="60">
                        <template slot-scope="scope">
                            <el-button type="text" icon="el-icon-plus" @click="addItem" v-if="scope.$index == 0"></el-button>
                            <el-button type="text" icon="el-icon-minus" @click="handleDelete(scope.row)" v-else></el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { addMaterial }  from '@/api/advertisement'
import { getShopList } from '@/api/shop'
import {  MATERIAL_PLATFORM_LIST, MATERIAL_TYPE_LIST } from '../../constant'
export default {
    name: 'MaterialDetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        type: {
            type: String,
            default: '',
        }
    },
    data() {
        return {
            model: {
                shop_id: '',
                spu: '',
                expert_id: '',
                expert_name: '',
                creation_code: '',
                material_type: 2,
                material_url: '',
                platform_list: []
            },
            rules: {
                expert_name: [{ required: true, message: '达人不能为空', trigger: ['blur', 'change'] }],
                spu: [{ required: true, message: 'spu不能为空', trigger: ['blur', 'change'] }],
                creation_code: [{ required: true, message: '达人不能为空', trigger: ['blur', 'change'] }],
                shop_id: [{ required: true, message: '店铺不能为空', trigger: ['blur', 'change'] }],
                material_url: [{ required: true, message: '网盘地址不能为空', trigger: ['blur', 'change'] }]
            },
            shopList: [],
            expertList: [],
            platformList: MATERIAL_PLATFORM_LIST,
            materialType: MATERIAL_TYPE_LIST,
            save: false,
        }
    },
    computed: {
        title() {
            return `${this.type == 'edit' ? '编辑' : '新增'}素材库`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
    },
        async created() {
        this.loading = true
        try {
            const [shopData] = await Promise.all([ getShopList()])
            this.shopList = shopData?.data?.list ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        if(this.type === 'add'){
            this.addItem()
        }
    },
    methods: {
         addItem() {
            this.model.platform_list.push({
                platform_id: '',
                platform_url: ''
            })
        },
        handleDelete(row) {
            this.$confirm('是否确认删除?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.model.platform_list = this.model.platform_list.filter(item => item.track_number !== row.track_number)
                    this.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            const { detailRequest } = this.currentInfo
            detailRequest(params)
                .then(res => {
                    this.model = Object.assign({}, this.model, res.data)
                    // this.model.status = res.status === 'normal' ? 1 : 0
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }
                const platform_list = this.model.platform_list.filter(item=>item.platform_id && item.platform_url)
                if(!platform_list.length) {
                   return this.$message.error('平台信息不能为空')
                }
                const params = { ...this.model }
                params.platform_list = platform_list
                // params.expert_name = this.expertList.find(item=>item.expert_id === this.model.expert_id).expert_name
                if (this.save) {
                    return
                }
                this.save = true
                if (this.type === 'edit') {
                    editRequest(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                } else {
                    addMaterial(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                }
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>

</style>
